<?php

/////////////////////////////////////////
# $template is the name of the template that will be called
# $return, default false, indicates whether the template function needs to write the data to the page when it's done, or return it for use in a variable
# $post, default FALSE, indicates whether the function will formatting data for blog posts, in which case it needs a seperate callback
# $id, default 0 is the ID of the blog post being formatted, only if $post is set to TRUE
/////////////////////////////////////////
function template($template,$return = false,$post = false,$id = 0)
{
	global $config;
	$string = file_get_contents("design/styles/".$config["admin"]["style"]."/templates/".$template.".tpl.php");
	if(($post) && $id !=0) {
		global $tempid;
		$tempid = $id;
		$string = html_entity_decode(preg_replace_callback("/\[\[([a-z -]*)?\]\]/i","post_template_callback",$string));
	} else {
		$string = html_entity_decode(preg_replace_callback("/\[\[([a-z -]*)?\]\]/i","template_callback",$string));
	}
	if($return) {	
		return $string;
	} else {
		echo $string;
	}
}

/////////////////////////////////////////
# $args is the output of the preg_replace_callback function in post_template(), and it should output the text within the placeholder
# e.g. $args[1] will be 'title' when it's replacing the [[title]] placeholder
# I don't completely understand it myself, but it works, and that's the main thing
/////////////////////////////////////////
function post_template_callback($args) {
	global $glob_posts;
	global $tempid;
	global $config;
	if($args[1] == 'date')
	{ return(date($config["admin"]["date-format"],$glob_posts[$tempid]["timestamp"])); }
	else
	{ return $glob_posts[$tempid][$args[1]]; }
}

/////////////////////////////////////////
# $args is the output of the preg_replace_callback function in post_template(), and it should output the text within the placeholder
# e.g. $args[1] will be 'title' when it's replacing the [[title]] placeholder
# I don't completely understand it myself, but it works, and that's the main thing
/////////////////////////////////////////
function template_callback($args) {
	global $config;
	if(substr($args[1],0,4) == "incl")
	{
		switch(substr($args[1],4))
		{
			case "content": require_once("php/posts.php"); return posts($config["admin"]["style"]); break;
			case "navigation": return template("navigation",true); break;
		}
	} else {
		return $config["text"][$args[1]];
	}
}

/////////////////////////////////////////
# $form_name is the name of the form that the textarea is attached to
# and $reply_name is the name of the textarea.
# This function simply outputs a list of the bbcode
/////////////////////////////////////////

function format_buttons($form_name,$reply_name)
{
			echo "<div class='bb_menus'>
			<div class='half'>
			<span class='bb_button bb-bold clickable' title='Makes text Bold' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[b][/b]')\">&nbsp;</span>
			<span class='bb_button bb-italic clickable' title='Makes text Italic' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[i][/i]')\">&nbsp;</span>
			<span class='bb_button bb-underline clickable' title='Makes text Underlined' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[u][/u]')\">&nbsp;</span>
			<span class='bb_button bb-strike clickable' title='Makes text struck through' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[s][/s]')\">&nbsp;</span>
			
			<span class='ui-icon ui-icon-extlink clickable' title='Inserts a link to a web page' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[url=http://address]title[/url]')\">&nbsp;</span>
			<span class='ui-icon ui-icon-image clickable' title='Inserts an Image from the internet' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[img]address[/img]')\">&nbsp;</span>
			<span class='ui-icon ui-icon-comment clickable' title='Adds a quote box' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[quote=person]quote[/quote]')\">&nbsp;</span>
			<span class='ui-icon ui-icon-video clickable' title='Inserts a video from YouTube' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[youtube]videoid[/youtube]')\">&nbsp;</span>
			</div>
			<div class='half'>
			<span class='bb_button emote-happy clickable' title='Happy Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", ':)')\"></span>
			<span class='bb_button emote-sad clickable' title='Sad Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", ':(')\"></span>
			<span class='bb_button emote-glee clickable' title='Glee Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '^_^')\"></span>
			<span class='bb_button emote-wut clickable' title='Lolwut? Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", 'O.o')\"></span>
			<span class='bb_button emote-nar clickable' title='Narrowed Eyes Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '-.-')\"></span>
			<span class='bb_button emote-confus clickable' title='Confused Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", ':S')\"></span>
			<span class='bb_button emote-cheeky clickable' title='Cheeky Face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", ':P')\"></span>
			<span class='bb_button emote-omg clickable' title='ZOMG face' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", ':O')\"></span>
			</div>
			<div>
			<a class='bb_font' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[font=arial]texthere[/font]')\" href='#x'><font face='arial'>Arial</font></a>
			<a class='bb_font' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[font=garamond]texthere[/font]')\" href='#x'><font face='garamond'>Garamond</font></a>
		    <a class='bb_font' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[font=courier]texthere[/font]')\" href='#x'><font face='courier'>Courier</font></a>
			
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#ffffff]texthere[/colour]')\" style='background-color:#ffffff;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#545454]texthere[/colour]')\" style='background-color:#545454;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#808080]texthere[/colour]')\" style='background-color:#808080;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#FF0000]texthere[/colour]')\" style='background-color:#FF0000;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#FFFF00]texthere[/colour]')\" style='background-color:#FFFF00;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#00FF00]texthere[/colour]')\" style='background-color:#00FF00;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#00FFFF]texthere[/colour]')\" style='background-color:#00FFFF;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#0000FF]texthere[/colour]')\" style='background-color:#0000FF;'>&nbsp;&nbsp;</a>
			<a class='bb_colour' href='#x' onClick=\"add_string(document." . $form_name . "." . $reply_name . ", '[colour=#FF00FF]texthere[/colour]')\" style='background-color:#FF00FF;'>&nbsp;&nbsp;</a>
			</div></div>";
}

?>